// pages/my/profile/edit/edit.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    current:"1",
    avatar:'',
    userInfo:{},
    sexList:[{value:'男',name:'男',checked:false},{value:'女',name:'女',checked:false},{value:'保密',name:'保密',checked:false}]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      current:options.current
    })
    this.getData()
  },
  //网络请求，获取数据
  getData(){
    //获取用户信息
    app.$http.get('/app/user/'+wx.getStorageSync('userId')).then(res =>{
      var sexList = this.data.sexList
      for (let i = 0, len = sexList.length; i < len; ++i) {
        sexList[i].value === res.data.sex ? sexList[i].checked = true : sexList[i].checked = false
      }
      this.setData({
        avatar:res.data.avatar
      })
      res.data.avatar = app.$http.baseImgUrl + "/avatar/" + res.data.avatar
      this.setData({
        userInfo:res.data,
        sexList,
      })
    }).catch(err => {
      console.log(err)
    })
  },
  //上传头像
  uploadAvatar: function() {
		var that = this;
		wx.showActionSheet({
			itemList: ['从相册中选择', '拍照'],
			itemColor: "#f7982a",
			success: function(res) {
				if (!res.cancel) {
					if (res.tapIndex == 0) {
						that.chooseWxImageShop('album'); //从相册中选择
					} else if (res.tapIndex == 1) {
						that.chooseWxImageShop('camera'); //手机拍照
					}
				}
			}
		})
  },
  //选择图片
  chooseWxImageShop: function(type) {
		var that = this;
		wx.chooseImage({
			sizeType: ['original', 'compressed'],
			sourceType: [type],
			success: function(res) {
				that.upload_file(res.tempFilePaths[0]);
			}
		})
  },
  //上传到图片服务器
  upload_file: function(filePath) {
		var that = this;
    var formData = {}; // HTTP 请求中其他额外的参数信息
		wx.uploadFile({
			url: "http://127.0.0.1:9091/file/upload/avatar", //后台处理接口
			filePath: filePath,
			name: 'file',
			header: {
        'Authorization': 'Bearer ' + wx.getStorageSync('login-token'),
				'content-type': 'multipart/form-data'
			}, // 设置请求的 header
			formData:formData, 
			success: function(res) {
        var data = JSON.parse(res.data).data;
        var submitForm = that.data.userInfo
        submitForm.avatar = data
        app.$http.put('/app/user/update',submitForm).then(resp=>{
          submitForm.avatar = app.$http.baseImgUrl + "/avatar/" + data
          that.setData({
            userInfo: submitForm,
          });
          wx.showToast({
            title: '上传成功',
            icon:'success'
          })
        }).catch(err=>{
          console.log(err)
        })
			},
      fail: function(res) {}
      
		})
  },
  //联系电话修改
  changeMobile(e){
    var submitForm = this.data.userInfo
    submitForm.mobile = e.detail.value
    this.setData({
      userInfo:submitForm
    })
  },
  changeNickname(e){
    var submitForm = this.data.userInfo
    submitForm.nickname = e.detail.value
    this.setData({
      userInfo:submitForm
    })
  },
  //改变性别单选框
  radioChange(e) {
    var submitForm = this.data.userInfo
    submitForm.sex = e.detail.value
    this.setData({
      userInfo:submitForm
    })
  },
  //改变日期选择器
  bindDateChange(e){
    var submitForm = this.data.userInfo
    submitForm.birthday = e.detail.value
    this.setData({
      userInfo:submitForm
    })
  },
  //修改住址
  changeAddress: function(e) {
    var submitForm = this.data.userInfo
    submitForm.address = e.detail.value
    this.setData({
      userInfo:submitForm
    })
  },
  //保存提交更改
  save(){
    let submitForm = this.data.userInfo
    submitForm.avatar = this.data.avatar
    app.$http.put('/app/user/update',submitForm).then(resp=>{
      wx.navigateBack()
    }).catch(err=>{
      console.log(err)
    })
  }
})